/*
 * CadastrodeTipodeProduto.java
 *
 * Created on 26 de Novembro de 2008, 19:26
 */
package br.faimg.pomar.visual;

import br.faimg.pomar.controle.componentevisual.TableTipodeProduto;
import javax.swing.JTable;
import javax.swing.table.TableModel;

/**
 *
 * @author  diogo
 */
public class CadastrodeTipodeProduto extends javax.swing.JInternalFrame {

    
    /** Creates new form CadastrodeTipodeProduto */
    public CadastrodeTipodeProduto() {
        initComponents();
        
        updateTable();
        

        alterarJButton1.setEnabled(false);
        
    }
    
    

    private void updateTable() {
        //Lista os itens cadastrados no banco na tablela
        tabelaTipojTable.setModel(new JTable(
                new TableTipodeProduto().getTableModel()).getModel());
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        codigoTextField = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        okButton = new javax.swing.JButton();
        cancelarButton = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        tabelaScrollPane = new javax.swing.JScrollPane();
        tabelaTipojTable = new javax.swing.JTable();
        alterarJButton1 = new javax.swing.JButton();

        codigoTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                codigoTextFieldActionPerformed(evt);
            }
        });
        codigoTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                codigoTextFieldKeyPressed(evt);
            }
        });

        setTitle("Cadastro de tipo de produto");

        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        okButton.setText("Incluir");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });
        jPanel1.add(okButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(350, 360, 80, -1));

        cancelarButton.setText("Cancelar");
        cancelarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelarButtonActionPerformed(evt);
            }
        });
        jPanel1.add(cancelarButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(540, 360, 90, -1));
        jPanel1.add(jSeparator1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 350, 620, 10));

        tabelaScrollPane.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tabelaScrollPaneMouseClicked(evt);
            }
        });

        tabelaTipojTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Código", "Descrição"
            }
        ));
        tabelaTipojTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tabelaTipojTableMouseClicked(evt);
            }
        });
        tabelaTipojTable.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tabelaTipojTableKeyPressed(evt);
            }
        });
        tabelaScrollPane.setViewportView(tabelaTipojTable);
        tabelaTipojTable.getColumnModel().getColumn(0).setMinWidth(60);
        tabelaTipojTable.getColumnModel().getColumn(0).setPreferredWidth(60);
        tabelaTipojTable.getColumnModel().getColumn(0).setMaxWidth(60);

        jPanel1.add(tabelaScrollPane, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, 620, 330));

        alterarJButton1.setText("Alterar");
        alterarJButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                alterarJButton1ActionPerformed(evt);
            }
        });
        jPanel1.add(alterarJButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(445, 360, 80, -1));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 644, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 394, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void codigoTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_codigoTextFieldActionPerformed
// TODO add your handling code here:
//GEN-LAST:event_codigoTextFieldActionPerformed

}                                               

private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed

 CadastrodeTipodeProdutoInclusao view = new CadastrodeTipodeProdutoInclusao();
 view.setModal(true);
 view.setVisible(true);
 view.setResizable(true);
 
 updateTable();
    

    }//GEN-LAST:event_okButtonActionPerformed
                                      

private void cancelarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelarButtonActionPerformed
   this.setVisible(isIcon);
    
   
}//GEN-LAST:event_cancelarButtonActionPerformed


private void tabelaTipojTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabelaTipojTableMouseClicked

    alterarJButton1.setEnabled(true);
    

    
}//GEN-LAST:event_tabelaTipojTableMouseClicked

private void codigoTextFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_codigoTextFieldKeyPressed
// TODO add your handling code here:
    
    cancelarButton.setText("Limpar");
   // erroEntradaDados.setText("");
}//GEN-LAST:event_codigoTextFieldKeyPressed

private void tabelaScrollPaneMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabelaScrollPaneMouseClicked
// TODO add your handling code here:
        if (tabelaTipojTable.getSelectedRow() >= 0){
            alterarJButton1.setEnabled(true);
        } else {
            alterarJButton1.setEnabled(false);
            
        }
    
}//GEN-LAST:event_tabelaScrollPaneMouseClicked

private void tabelaTipojTableKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tabelaTipojTableKeyPressed
// TODO add your handling code here:
}//GEN-LAST:event_tabelaTipojTableKeyPressed

private void alterarJButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_alterarJButton1ActionPerformed
    
    TableModel tableModel = tabelaTipojTable.getModel();
    int linha = tabelaTipojTable.getSelectedRow();
     
    CadastrodeTipodeProdutoInclusao view = new CadastrodeTipodeProdutoInclusao(
            (String)tableModel.getValueAt(linha,0), (String)tableModel.getValueAt(linha,1));
   
    view.setModal(true);
    view.setResizable(false);
    view.setVisible(true);
    

    updateTable();

    
}//GEN-LAST:event_alterarJButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton alterarJButton1;
    private javax.swing.JButton cancelarButton;
    private javax.swing.JTextField codigoTextField;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JButton okButton;
    private javax.swing.JScrollPane tabelaScrollPane;
    private javax.swing.JTable tabelaTipojTable;
    // End of variables declaration//GEN-END:variables
}
